<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('名称')" prop="keyWord">
            <el-input v-model="form.keyWord" :placeholder="$t('名称')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('唯一标识')" prop="code">
            <el-input v-model="form.code" :placeholder="$t('唯一标识')" show-word-limit maxlength="125" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('版本')" prop="version">
            <el-input v-model="form.version" :placeholder="$t('版本')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('sql')" prop="sqlScript">
            <codemirror ref="sqlScript" v-model="form.sqlScript" :options="cmOptions" class="codesql" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <!-- <save-btn has-permi="system:word" /> -->
    </div>
  </div>
</template>

<script>
    import { saveWordOnlineKeyWordVersion, getWordOnlineKeyWordVersion } from '@/api/system/word/wordOnlineKeyWordVersion'
    import 'codemirror/theme/ambiance.css'
    import 'codemirror/lib/codemirror.css'
    import 'codemirror/addon/hint/show-hint.css'
    import { codemirror } from 'vue-codemirror'

    require('codemirror/addon/edit/matchbrackets')
    require('codemirror/addon/selection/active-line')
    require('codemirror/mode/sql/sql')
    require('codemirror/addon/hint/show-hint')
    require('codemirror/addon/hint/sql-hint')
    require('codemirror/addon/display/autorefresh')
    export default {
      components: {
        codemirror
      },
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                },
                cmOptions: {
                  autorefresh: true,
                  tabSize: 2,
                  mode: 'text/x-mariadb',
                  // theme: 'ambiance',
                  indentWithTabs: true,
                  smartIndent: true,
                  lineNumbers: true,
                  matchBrackets: true,
                  readOnly: this.isBuiltin,
                  extraKeys: { Ctrl: 'autocomplete' }, // 自定义快捷键
                  hintOptions: { // 自定义提示选项
                    tables: {
                      users: ['name', 'score', 'birthDate'],
                      countries: ['name', 'population', 'size']
                    }
                  }
                }
            }
        },
        created() {
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getWordOnlineKeyWordVersion(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    code: undefined,
                    keyId: undefined,
                    version: undefined,
                    parentId: undefined,
                    keyWord: undefined,
                    sqlScript: undefined,
                    createBy: undefined,
                    updateBy: undefined,
                    createTime: undefined,
                    updateTime: undefined,
                    delFlag: undefined,
                    dataType: undefined
                }
                this.$refs.sqlScript.codemirror.setValue('')
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveWordOnlineKeyWordVersion(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                }).catch(() => {
                    this.loading = false
                })
           }
        }
    }
</script>
